package com.pdh.service;

import com.pdh.bean.Message;
import com.pdh.dao.BaseAdminDao;
import com.pdh.dao.imp.AdminDaoMysql;
import com.pdh.exception.UnHandleException;
import com.pdh.util.LoggerUtil;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class AdminService {
    private static BaseAdminDao dao = new AdminDaoMysql();

    /**
     * 更新登陆时间与ip
     * @param adminName
     * @param ip
     */
    public static Message updateLoginInformation(String adminName, String ip){
        boolean flag = false;
        Message msg = null;
        try {
            flag = dao.updateLoginInformation(adminName,ip);
            if(flag){
                msg = new Message(0,"更新成功");
            }else {
                msg = new Message(-1,"更新失败");
            }
        } catch (UnHandleException e) {
            msg = new Message(-1, "未知异常");
            LoggerUtil.getLogger(AdminService.class).error(e.getMessage());
        }
        return msg;
    }

    /**
     * 登陆
     * @param adminName
     * @param password
     * @return 管理员的手机号  存在就登陆成功/反之登陆失败
     */
    public static Message login(String adminName,String password) {
        Message msg = null;
        String adminPhone = null;
        try {
            adminPhone = dao.login(adminName, password);
            if (adminPhone != null) {//登陆成功
                msg = new Message(0, "登陆成功",adminPhone);
            } else {
                msg = new Message(-1, "请检查管理员名称和密码");
            }
        } catch (UnHandleException e) {
            msg = new Message(-1, "未知异常");
            LoggerUtil.getLogger(AdminService.class).error(e.getMessage());
        }
        return msg;
    }
}
